﻿using System.Collections.Generic;
using System.Linq;
using Model.Entities;
using Model.Repositories;

namespace Presenter.Groups
{
    public class GroupMessagesPresenter
    {
        private IGroupMessagesView _view;

        public GroupMessagesPresenter(IGroupMessagesView view, int id, string email, string password)
        {
            _view = view;

            _view.GetMessages(GetMessages(id));
            _view.EntityCreated += ViewEntityCreated;

            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                _view.CanCreateMessages = obj.Groups.FirstOrDefault(o => o.Id == id) != null || obj.OwnedGroups.FirstOrDefault(o => o.Id == id) != null;
            }
            else
                _view.CanCreateMessages = false;
        }

        void ViewEntityCreated(string email, string password, int id, string title, string text)
        {
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                var group = RepositoryManager.Instance.GroupRepository.Get(id);
                if (group != null)
                {
                    group.GroupMessages.Add(new GroupMessage
                                                {
                                                    Group = RepositoryManager.Instance.GroupRepository.Get(id),
                                                    Sender = obj,
                                                    Text = text,
                                                    Title = title
                                                });
                    RepositoryManager.Instance.GroupRepository.Update(group);
                }
            }
        }

        public void Reload(IGroupMessagesView view, int id, string email, string password)
        {
            _view = view;

            _view.GetMessages(GetMessages(id));
            _view.EntityCreated += ViewEntityCreated;
            
            var obj = RepositoryManager.Instance.UserRepository.GetByEmailAndPassword(email, password);
            if (obj != null)
            {
                _view.CanCreateMessages = obj.Groups.FirstOrDefault(o => o.Id == id) != null || obj.OwnedGroups.FirstOrDefault(o => o.Id == id) != null;
            }
            else
                _view.CanCreateMessages = false;
        }

        List<List<string>> GetMessages(int id)
        {
            var result = new List<List<string>>();

            var obj = RepositoryManager.Instance.GroupRepository.Get(id);
            if (obj != null)
            {
                result.AddRange(obj.GroupMessages.Select(message => new List<string> {message.Id.ToString(), message.Sender.Id.ToString(), message.Title, message.Text, message.SentDateTime.ToString()}));
            }

            return result;
        }
    }
}

